//
//  CallViewController.m
//  JinChengYun
//
//  Created by 金橙云 on 2017/2/28.
//  Copyright © 2017年 金橙云. All rights reserved.
//

#import "CallViewController.h"

#import "CallPhoneCell.h"
#import "CallDetailsViewController.h"
#import "TodoHttpManager.h"

#import "NewGenJinVC.h"
#import "LaiDianCell.h"
#import "EJCallViewController.h"
@interface CallViewController ()<UITableViewDelegate,UITableViewDataSource>

{
    NSMutableArray *_kePhoneArr;
    NSMutableDictionary *_dianDic;
    NSString *uid;
    NSString *nameStr;
}
@property(nonatomic,strong)UITableView *tabView;
@end

@implementation CallViewController

-(void)viewWillAppear:(BOOL)animated
{
  
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDGuPhone:) name:TDGuPhone object:nil];
    NSUserDefaults *userDefa = [NSUserDefaults standardUserDefaults];
    
    uid= [userDefa objectForKey:@"USER_ID"];
    [_manager userGuWenPhoneID:uid withSouNei:nil withDengJi:nil withKeState:nil withPaiFang:nil];
}

-(void)didTDGuPhone:(NSNotification*)notifi
{
    if (notifi.object != nil)
    {
        int status = [[notifi.object objectForKey:@"status"] intValue];
        if (status == 1)
        {
            _dianDic = [notifi.object objectForKey:@"data"];
            _kePhoneArr = [_dianDic objectForKey:@"data"];
            
            [_tabView reloadData];
        }
        
        
    }
    


}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDGuPhone object:nil];
     [[NSNotificationCenter defaultCenter] removeObserver:self name:@"Call" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"CallClear" object:nil];
      [[NSNotificationCenter defaultCenter] removeObserver:self name:@"CellLouDou" object:nil];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BgColor;
  
    _isCall = YES;
    _kePhoneArr = [NSMutableArray arrayWithCapacity:0];
    _dianDic = [[NSMutableDictionary alloc] init];
    _manager = [TodoHttpManager sharedManager];
    _tabView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, P_WIDTH, P_HEIGHT-45-44-LL_TabbarHeight-LL_StatusBarAndNavigationBarHeight) style:UITableViewStylePlain];
    _tabView.delegate = self;
    _tabView.dataSource = self;
    _tabView.rowHeight = 70;
    _tabView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tabView];
    //TODO:搜索框通知
    NSNotificationCenter *notiCenter = [NSNotificationCenter defaultCenter];
    [notiCenter addObserver:self selector:@selector(callNotification:) name:@"Call" object:nil];
    [notiCenter addObserver:self selector:@selector(callClearNotification:) name:@"CallClear" object:nil];
     //TODO:漏斗通知
     [notiCenter addObserver:self selector:@selector(callLouDNotification:) name:@"CellLouDou" object:nil];
    _tabView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(callRefersh)];
 
}



#pragma mark-----搜索框的通知方法
- (void)callNotification:(NSNotification *)noti
{
    
    // NSNotification 有三个属性，name, object, userInfo，其中最关键的object就是从第三个界面传来的数据。name就是通知事件的名字， userInfo一般是事件的信息。
    //NSLog(@"%@ === %@ === %@", noti.object, noti.userInfo, noti.name);
    nameStr = noti.object;
    //TODO:来访客户
     [_manager userGuWenPhoneID:uid withSouNei:nameStr withDengJi:nil withKeState:nil withPaiFang:nil];
    //NSLog(@"=======%@",nameStr);
    [_tabView reloadData];
    if ([noti.object isEqualToString:@""])
    {
        //TODO:来访客户
    [_manager userGuWenPhoneID:uid withSouNei:nameStr withDengJi:nil withKeState:nil withPaiFang:nil];
        [_tabView reloadData];
    }
    
}
- (void)callClearNotification:(NSNotification *)nf {
    nameStr = @"";
}
 #pragma mark========= 漏斗的通知方法 ========
-(void)callLouDNotification:(NSNotification *)notifi
{
//    if ([notifi.object objectForKey:@"XX"] != nil) {
//        <#statements#>
//    }
    NSString *oneStr = [notifi.object objectForKey:@"XX"];
    NSString *twoStr = [notifi.object objectForKey:@"YY"];
    NSString *threeStr = [notifi.object objectForKey:@"ZZ"];
    NSLog(@"------11111------%@",oneStr);
    NSLog(@"---------22222-------%@",twoStr);
  [_manager userGuWenPhoneID:uid withSouNei:nil withDengJi:oneStr withKeState:twoStr withPaiFang:threeStr];
    [_tabView reloadData];
}

#pragma mark--刷新---------刷新--------


-(void)callRefersh
{
    if (nameStr.length == 0) {
        [_manager userGuWenPhoneID:uid withSouNei:nil withDengJi:nil withKeState:nil withPaiFang:nil];
    }else {
        [_manager userGuWenPhoneID:uid withSouNei:nameStr withDengJi:nil withKeState:nil withPaiFang:nil];
    }
    [self.tabView reloadData];
    [self.tabView.mj_header endRefreshing];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _kePhoneArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndent = @"Cell";
    LaiDianCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndent];
    if (!cell)
    {
        cell = [[LaiDianCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndent];
    }
    if ([cell isKindOfClass:[LaiDianCell class]])
    {
        if (_kePhoneArr.count != 0)
        {
            NSDictionary *dict = [_kePhoneArr objectAtIndex:indexPath.row];
            
            [(LaiDianCell *)cell setUpVisitCellWithImg:nil withName:dict[@"name"] withSex:dict[@"sex"] withFen:dict[@"khdj"] withFirstVisit:dict[@"time"] withGen:dict[@"genjin_time"] withBei:dict[@"genjin_data"] withPhone:dict[@"phone"]withChengJiaoState:dict[@"cjzt"]withCallStr:nil ];
            
        }
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;//取消选择效果
    
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
//    CallDetailsViewController *callDetailVC = [[CallDetailsViewController alloc] init];
//    callDetailVC.callID = [[_kePhoneArr objectAtIndex:indexPath.row] objectForKey:@"id"];
//    [self.navigationController pushViewController:callDetailVC animated:NO];
//    NewGenJinVC *newVC = [[NewGenJinVC alloc] init];
//    newVC.genID = [[_kePhoneArr objectAtIndex:indexPath.row] objectForKey:@"id"];
//    newVC.ORCall = _isCall;
//    [self.navigationController pushViewController:newVC animated:NO];
    
    EJCallViewController *ejCallVC = [[EJCallViewController alloc] init];
    ejCallVC.genID = [[_kePhoneArr objectAtIndex:indexPath.row] objectForKey:@"id"];
    ejCallVC.ORCall = _isCall;
    [self.navigationController pushViewController:ejCallVC animated:NO];
    
}













- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
